package com.czy.owner.ui.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.czy.owner.R;
import com.czy.owner.entity.MessageContacts;
import com.czy.owner.global.Constants;
import com.czy.owner.utils.StatusBarUtil;
import com.easemob.cases.ui.ChatFragment;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseBaseActivity;
import com.hyphenate.easeui.widget.EaseTitleBar;

/**
 * Created by King on 2016/8/5 0005.
 */
public class ChatActivity extends EaseBaseActivity {
    public static ChatActivity activityInstance;
    private ChatFragment chatFragment;
    private String toChatUsername;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        //create our manager instance after the content view is set
        setContentView(R.layout.activity_chat);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.appThemeColor));
        //ActivityHelper.getInstance().pushOneActivity(this);

        activityInstance = this;

        toChatUsername = getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID);

        chatFragment = new ChatFragment();
        //set arguments
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.chat_container, chatFragment).commit();

        if(chatFragment.getView()==null){
            Log.i("King","getView is null");
        }else{
            EaseTitleBar titleBar = (EaseTitleBar)(chatFragment.getView().findViewById(R.id.title_bar));
            titleBar.setBackgroundColor(ContextCompat.getColor(this, R.color.appThemeColor));
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //this.unregisterReceiver(this.finishAppReceiver);
        //ActivityHelper.getInstance().popOneActivity(this);
        activityInstance = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // enter to chat activity when click notification bar, here make sure only one chat activiy
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }


    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }



}
